package com.cooba.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.GoodsExchangeBean;
import com.cooba.utils.ServerURL;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

public class GoodsExchangeAdapter extends BaseAdapter implements Filterable {
    private final BitmapUtils bitmapUtils;
    private Context context;
    private List<GoodsExchangeBean.productsInfo> list;
    private DataFilter filter;

    public List<GoodsExchangeBean.productsInfo> getList() {
        return list;
    }

    public void setList(List<GoodsExchangeBean.productsInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addNewData(List<GoodsExchangeBean.productsInfo> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<GoodsExchangeBean.productsInfo> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public GoodsExchangeAdapter(Context context, List<GoodsExchangeBean.productsInfo> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        /**
         * 设置默认的图片展现、加载失败的图片展现
         */
        bitmapUtils.configDefaultLoadingImage(R.mipmap.default_icon1);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.default_icon1);
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.goods_exchange_list_item, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.tv_tittle);
            holder.desc = (TextView) convertView.findViewById(R.id.goods_exchange_list_item_desc);
            holder.integral_num = (TextView) convertView.findViewById(R.id.goods_exchange_list_item_need_integral);
            holder.num = (TextView) convertView.findViewById(R.id.goods_exchange_list_item_num);
            holder.ex_type= (TextView) convertView.findViewById(R.id.goods_exchange_list_item_ex_type);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.exchange_btn= (TextView) convertView.findViewById(R.id.goods_exchange_list_item_exchange_bt);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        holder.title.setText(list.get(position).getName());
        holder.desc.setText(list.get(position).getDesc());
        String integralnum=context.getString(R.string.integral_need_num,list.get(position).getIntegral_num());
        holder.integral_num.setText(Utils.getColorSize(context,integralnum,5,integralnum.length(),R.color.limegreen,12));
        holder.num.setText("剩余:"+list.get(position).getNum());
        String num=context.getString(R.string.num,list.get(position).getNum());
        holder.num.setText(Utils.getColorSize(context, num, 3, num.length(), R.color.red, 12));
        holder.ex_type.setText(list.get(position).getEx_type());
        bitmapUtils.display(holder.img, ServerURL.host + list.get(position).getIcon());
        if (list.get(position).getNum().equals("0")){
            holder.exchange_btn.setBackground(context.getResources().getDrawable(R.drawable.round_bg_gray));
        }else {
            holder.exchange_btn.setBackground(context.getResources().getDrawable(R.drawable.round_bg));
        }

        return convertView;
    }


    class ViewHolder {
        TextView title;
        TextView desc;
        TextView ex_type;//兑换规则
        TextView integral_num;//兑换积分
        TextView num;//剩余数量
        ImageView img;
        TextView exchange_btn;

    }
    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new DataFilter(list);
        }

        return filter;
    }
    private class DataFilter extends Filter {

        private List<GoodsExchangeBean.productsInfo> original;

        public DataFilter(List<GoodsExchangeBean.productsInfo> list) {
            this.original = list ;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = original;
                results.count = original.size();
            } else {
                List<GoodsExchangeBean.productsInfo> mList = new ArrayList<>();
                for(int i=0; i<original.size(); i++){
                    if (original.get(i).getType().equals(constraint.toString())) {
                        mList.add(original.get(i));
                    }
                }
                results.values = mList;
                results.count = mList.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            list = (List<GoodsExchangeBean.productsInfo>)results.values;
            notifyDataSetChanged();
        }

    }
}
